package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealServiceMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 符健清
 * @version 1.0
 * 套餐接口   实现类
 */
@SuppressWarnings({"all"})
@Slf4j
@Component
public class SetMealServicempl implements SetMealService {
    @Autowired
    private SetMealServiceMapper setMealServiceMapper;

    /**
     * 分页条件查询
     *
     * @param setmealPageQueryDTO
     */
    @Override
    public PageResult pageSelectSetMeal(SetmealPageQueryDTO setmealPageQueryDTO) {
//        使用分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

//        添加查询
//       使用  setmeal 类型进行访问数据库
        Setmeal setmeal = new Setmeal();
//         使用beautils 工具进行赋值
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);

        List<Setmeal> lists = setMealServiceMapper.pageSelectSetmeal(setmeal);

//        封装  返回
//        数据强转
        Page<Setmeal> page = (Page<Setmeal>) lists;
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 起售 /停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startAndStopSetmeal(Integer status, Integer id) {
        setMealServiceMapper.startAndStopSetmeal(status, id);
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    public void deleteSetmeal(List<Integer> ids) {
        setMealServiceMapper.deleteSetmeal(ids);
    }


    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
//        使用 Setmeal 实体类访问数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
//        修改其他参数
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(10L);
        setmeal.setUpdateUser(10L);
//       调用mapper 层添加数据
        setMealServiceMapper.inserSetmeal(setmeal);
//        查询 添加套餐的id
      List<Setmeal> listSetmeal  =setMealServiceMapper.selectSetmealByName(setmeal.getName());
//      获取id
        Setmeal  insetSetmeal=null;
        if(listSetmeal.size()>0){
        insetSetmeal = listSetmeal.get(0);
        }
        log.info("insertSetmeal id:{}" ,insetSetmeal.getId());
//      取出setmealDishes
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0)  {
//            绑定物理外键
            SetmealDish setmealDish = setmealDishes.get(0);
//            套餐id
            setmealDish.setSetmealId(insetSetmeal.getId());
//         添加数据
            setMealServiceMapper.insertSetmealDish(setmealDish);
        }

    }

    /**
     * 根据id  查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDTO selectSetmealById(Integer id) {
//         查询  setmeal 表格
        Setmeal setmeal = setMealServiceMapper.selectSetmealById(id);

//        查询 setmeal_dish 表格
        SetmealDish setmealDish = setMealServiceMapper.selectSetmealDishById(id);

//        封装 成SetmealDTO  数据返回
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal, setmealDTO);
        List<SetmealDish> setmealDishes = new ArrayList<SetmealDish>();
        setmealDishes.add(setmealDish);
        setmealDTO.setSetmealDishes(setmealDishes);

        return setmealDTO;
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {

    //  取出setmeal 参数
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
//        修改其他参数
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(10L);
        setmeal.setCreateUser(10L);
//        调用mapper 层
        setMealServiceMapper.updateSetmeal(setmeal);

        //   取出 setmeal_dish 参数
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        SetmealDish setmealDish = setmealDishes.get(0);
//        调用mapper层
           setMealServiceMapper.updateSetmealDish(setmealDish);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealServiceMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealServiceMapper.getDishItemBySetmealId(id);
    }
}
